<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>状态压缩动态规划(状压DP)详解 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="状压DP。">
<meta property="og:type" content="article">
<meta property="og:title" content="状态压缩动态规划(状压DP)详解">
<meta property="og:url" content="http://example.com/2020/08/06/%E7%8A%B6%E6%80%81%E5%8E%8B%E7%BC%A9%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92(%E7%8A%B6%E5%8E%8BDP)%E8%AF%A6%E8%A7%A3/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="状压DP。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-06T06:38:10.493Z">
<meta property="article:modified_time" content="2023-07-19T11:51:09.491Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-状态压缩动态规划(状压DP)详解" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/08/06/%E7%8A%B6%E6%80%81%E5%8E%8B%E7%BC%A9%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92(%E7%8A%B6%E5%8E%8BDP)%E8%AF%A6%E8%A7%A3/" class="article-date">
  <time class="dt-published" datetime="2020-08-06T06:38:10.493Z" itemprop="datePublished">2020-08-06</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      状态压缩动态规划(状压DP)详解
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>状压DP。</p>
<h1 id="0-引子"><a href="#0-引子" class="headerlink" title="0 引子"></a>0 引子</h1><p>在计算机里，整数是以二进制的方式存储的。把状态信息压缩成二进制当成状态进行动态规划，就是状压DP的基本思想。</p>
<p>是不是一脸懵比？别急着关掉文章，接着往下看，<del>你会发现一个新世界。</del></p>
<h1 id="0-5-状压能解决什么样的问题？"><a href="#0-5-状压能解决什么样的问题？" class="headerlink" title="0.5 状压能解决什么样的问题？"></a>0.5 状压能解决什么样的问题？</h1><p>让我们康康这道题：<a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P1441">传送门</a></p>
<p>很容易想到搜索，不是吗？</p>
<p>然而，我们要用更<del>装比</del> <del>复杂</del> 优美的方式完成这道题。</p>
<h1 id="1-什么是“状态压缩？”"><a href="#1-什么是“状态压缩？”" class="headerlink" title="1.什么是“状态压缩？”"></a>1.什么是“状态压缩？”</h1><p>举个例子吧。例如，例题里的“取哪些砝码”这个信息，就可以压缩进一个int整数里。因为，int整数是二进制存储的。大概长这样：</p>
<script type="math/tex; mode=display"> </script><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:right">十进制</th>
<th style="text-align:right">二进制（也就是计算机里实际的状态）</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right">1</td>
<td style="text-align:right">1</td>
</tr>
<tr>
<td style="text-align:right">2</td>
<td style="text-align:right">10</td>
</tr>
<tr>
<td style="text-align:right">114514</td>
<td style="text-align:right">11011111101010010</td>
</tr>
<tr>
<td style="text-align:right">6</td>
<td style="text-align:right">110</td>
</tr>
</tbody>
</table>
</div>
<p>我们可以发现，数字是由许多二进制位构成的，要么是0要么是1。</p>
<p>是不是发现了什么？</p>
<p><strong>没错，我们可以用每一位的0和1表示每一个砝码选或者不选！</strong> </p>
<p>例如，数字 2 （二进制：10） 就可以表示1号砝码不选，2号砝码选的状态。（因为第1位是0，第2位是1）。</p>
<p>知道了如何压缩，就可以用动态规划的方式求解了。</p>
<h1 id="2-如何枚举出每一个状态？"><a href="#2-如何枚举出每一个状态？" class="headerlink" title="2.如何枚举出每一个状态？"></a>2.如何枚举出每一个状态？</h1><p>事实上十分简单，这样就可以：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;=(<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>;i++)&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>（1&lt;&lt;n是把1左移n位的意思。例如，1&lt;&lt;3的二进制就是1000，十进制就是8）</p>
<p>我们来模拟一下$n=3$时的情况。</p>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:right">i</th>
<th style="text-align:right">i的二进制</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right">0</td>
<td style="text-align:right">000</td>
</tr>
<tr>
<td style="text-align:right">1</td>
<td style="text-align:right">001</td>
</tr>
<tr>
<td style="text-align:right">2</td>
<td style="text-align:right">010</td>
</tr>
<tr>
<td style="text-align:right">3</td>
<td style="text-align:right">011</td>
</tr>
<tr>
<td style="text-align:right">4</td>
<td style="text-align:right">100</td>
</tr>
<tr>
<td style="text-align:right">5</td>
<td style="text-align:right">101</td>
</tr>
<tr>
<td style="text-align:right">6</td>
<td style="text-align:right">110</td>
</tr>
<tr>
<td style="text-align:right">7</td>
<td style="text-align:right">111</td>
</tr>
</tbody>
</table>
</div>
<p>可以发现，i把所有砝码选取情况都枚举出来了。</p>
<h1 id="3-如何转移？"><a href="#3-如何转移？" class="headerlink" title="3.如何转移？"></a>3.如何转移？</h1><p>这个其实因题而异，我就用这道题举例子吧</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;(<span class="number">1</span>&lt;&lt;n);i++)&#123;<span class="comment">//枚举。因为i=0就是一个也不选，所以不需要枚举。</span></span><br><span class="line">    <span class="type">int</span> high_bit=<span class="number">0</span>,high_bit_num=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">31</span>;j&gt;=<span class="number">0</span>;j--)&#123;<span class="comment">//int最多有31位，所以j=31-0。</span></span><br><span class="line">        <span class="keyword">if</span>((i&gt;&gt;j)&amp;<span class="number">1</span>)&#123;<span class="comment">//意思是i的第j位是否为1</span></span><br><span class="line">            high_bit=<span class="number">1</span>&lt;&lt;j;</span><br><span class="line">            high_bit_num=j;</span><br><span class="line">            <span class="keyword">break</span>;<span class="comment">//找到了就退出来</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="comment">//求出当前方案的最高位</span></span><br><span class="line">    sum[i]=sum[i^high_bit]+a[high_bit_num+<span class="number">1</span>];<span class="comment">//转移。因为i是“按顺序”枚举的，所以去掉最高位后的方案一定枚举过了。</span></span><br><span class="line">    <span class="comment">//i^high_bit的意思是去掉最高位</span></span><br><span class="line">    cnt[i]=cnt[i^high_bit]+<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>（这段代码求的是每种砝码选取方案的重量和与每种砝码选取方案要用多少砝码）</p>
<p>注释写的很详细，我就不再罗嗦了。</p>
<h1 id="4-如果你想A掉这道题"><a href="#4-如果你想A掉这道题" class="headerlink" title="4.如果你想A掉这道题"></a>4.如果你想A掉这道题</h1><p>你可以康康我的题解：<a target="_blank" rel="noopener" href="https://www.luogu.com.cn/blog/ztx666-blog/post-zhuang-tai-ya-su-dpp1441-fa-ma-cheng-zhong">传送门</a></p>
<p>但是我还是建议你自己做出来</p>
<h1 id="5-写在最后"><a href="#5-写在最后" class="headerlink" title="5.写在最后"></a>5.写在最后</h1><p>状压dp和其它所有算法一样，需要大量的练习才能掌握。</p>
<p>这篇文章写的可能不怎么全面，欢迎在评论区提问或指出。另外，不要吊死在一棵树上，多康康别人的文章吧。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/08/06/%E7%8A%B6%E6%80%81%E5%8E%8B%E7%BC%A9%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92(%E7%8A%B6%E5%8E%8BDP)%E8%AF%A6%E8%A7%A3/" data-id="clk9o8ln9000i8wi5f2l53oqy" data-title="状态压缩动态规划(状压DP)详解" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/08/06/SPFA%E7%AE%97%E6%B3%95%E8%AF%A6%E8%A7%A3/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          SPFA算法详解
        
      </div>
    </a>
  
  
    <a href="/2020/08/06/%E3%80%8C%E7%8A%B6%E5%8E%8BDP%E3%80%8DP1441%20%E9%A2%98%E8%A7%A3%20%E7%A0%9D%E7%A0%81%E7%A7%B0%E9%87%8D/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">「状压DP」P1441 题解 砝码称重</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>